package cn.calm.message.service;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;

import org.apache.log4j.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import cn.calm.commons.dao.IBaseDao;
import cn.calm.entity.Message;
import cn.calm.message.service.impl.MessageServiceImpl;

public class Activator implements BundleActivator {
	public static BundleContext context;
	private static Logger logger = Logger.getLogger(Activator.class);

	@Override
	public void start(BundleContext arg0) throws Exception {
		logger.debug("starting");
		try {

			context = arg0;
			IMessageService<Message> service = new MessageServiceImpl<Message>();

			ServiceReference<IBaseDao> serviceRef = context
					.getServiceReference(IBaseDao.class);
			IBaseDao dao = context.getService(serviceRef);
			EntityManager em = Persistence.createEntityManagerFactory("entity")
					.createEntityManager();
			dao.setSession(em);
			service.setBaseDao(dao);
			arg0.registerService(IMessageService.class.getName(), service, null);
		} catch (Exception e) {
			logger.error("",e);
		} finally {
			logger.debug("started");
		}

	}

	@Override
	public void stop(BundleContext arg0) throws Exception {

	}

}
